package me.atbar.moonlib.util;

import me.atbar.moonlib.manager.SoftDependerEnum;
import org.black_ixx.playerpoints.PlayerPoints;
import org.black_ixx.playerpoints.PlayerPointsAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.Objects;
import java.util.UUID;

public class PointUtils {

    private final static boolean HAS_POINT = setUpPoint();
    private static PlayerPointsAPI pointsAPI = null;

    private static boolean setUpPoint() {
        if (Bukkit.getServer().getPluginManager().getPlugin(SoftDependerEnum.PLUGIN_POINTS.get()) == null) {
            return false;
        }
        PlayerPoints playerPoints = (PlayerPoints)Bukkit.getServer().getPluginManager().getPlugin("PlayerPoints");
        if(playerPoints != null) {
            pointsAPI = playerPoints.getAPI();
        }
        return true;
    }

    private PointUtils() {}

    public static void add(Player player, int amount) {
        if(!HAS_POINT) {
            return;
        }
        UUID uuid = player.getUniqueId();
        pointsAPI.give(uuid, amount);
    }

    public static void remove(Player player, int amount) {
        if(!HAS_POINT) {
            return;
        }
        if(has(player, amount)) {
            UUID uuid = player.getUniqueId();
            pointsAPI.take(uuid, amount);
        }
    }

    public static void reset(Player player) {
        if(!HAS_POINT) {
            return;
        }
        UUID uuid = player.getUniqueId();
        pointsAPI.reset(uuid);
    }

    public static String info(Player player) {
        if(!HAS_POINT) {
            return null;
        }
        UUID uuid = player.getUniqueId();
        return String.valueOf(pointsAPI.look(uuid));
    }

    public static boolean has(Player player, int amount) {
        if(!HAS_POINT) {
            return false;
        }
        int playerPoint = Integer.parseInt(Objects.requireNonNull(info(player)));
        return playerPoint > amount;
    }

}
